#ifndef __UserLoginSection1_HPP__
#define __UserLoginSection1_HPP__

#include "MySQLClient.hpp"
#include "TcpConnection.hpp"
#include "Packet.hpp"
#include "TLV.hpp"
#include "logger.hpp"

class UserLoginSection1
{
public:
    UserLoginSection1(wd::TcpConnectionPtr conn,Packet pack ):_conn(conn),_packet(pack){

    }
    ~UserLoginSection1() {}

    void process(){
        using namespace wd;
        using namespace std;
        //1.解码
        //(1)判断类型
        if(_packet.type != TASK_LOGIN_SECTION1){
            MyLogger::getInstance().serverError("Packet type error!");
            return;
        }
        //(2)获得用户名
        string username = _packet._msg;
        MyLogger::getInstance().serverInfo("用户登录请求: "+ username);
        
        //2.MySQL查询,获取盐值
        wd::MySQLClient myclient;
        bool ret = myclient.connect("localhost", 3306, "root", "1234",
                                "shms");
        if(!ret) {
            exit( EXIT_FAILURE);
        }
        
        printf("--------------38-----------\n"); 
        string setting = myclient.getSetting(username);        
        
        printf("--------------40-----------\n"); 
        //3.编码tlv
        
        TLV tlv;
        if(setting == ""){
            tlv.type = TASK_LOGIN_SECTION1_RESP_ERROR;
            tlv.length = 0;
        }else{
            printf("--------------80-----------\n"); 
            tlv.type = TASK_LOGIN_SECTION1_RESP_OK;
            tlv.length = setting.size();
            tlv.setData(setting);
        }
        //4.发送到eventloop中

        _conn->sendInLoop(tlv);
    }
private:
    wd::TcpConnectionPtr _conn;
    Packet _packet; 
};

#endif
